package com.jy.app.lib.view.custom;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jy.app.lib.R;


/**
 * Created by tm on 2016/3/22.
 */
public class TitleView extends LinearLayout{


    private TextView tv_title;
    private TextView tv_right;
    private TextView tv_left;
    private RelativeLayout rl_view;

    private ImageView iv_left;
    private ImageView iv_right;

    private boolean show_tv_title = true;
    private boolean show_tv_right = false;
    private boolean show_tv_left = false;
    private boolean show_iv_left = true;
    private boolean show_iv_right = false;

    private String tv_title_content = "";
    private String tv_right_content = "";
    private String tv_left_content = "";
    private String titleBackground = "";
    private String titleTextColor = "";
    private int left_image = 0;
    private int right_image = 0;

    private OnClickListener onTvTitleClickListener = null;
    private OnClickListener onTvRightClickListener = null;
    private OnClickListener onTvLeftClickListener = null;
    private OnClickListener onIvLefteClickListener = null;
    private OnClickListener onIvRightClickListener = null;

    private Context context;

    public TitleView(Context context) {
        this(context, null);

    }

    public TitleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setOrientation(LinearLayout.VERTICAL);
        this.context = context;
        initAttributes(attrs);
        initviews(context);
    }

    private void initAttributes(AttributeSet attrs){
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TitleView);
        show_tv_right = typedArray.getBoolean(R.styleable.TitleView_showRightText, true);
        show_iv_right = typedArray.getBoolean(R.styleable.TitleView_showRightImg, false);
        show_iv_left = typedArray.getBoolean(R.styleable.TitleView_showLeftImg, true);
        tv_title_content = typedArray.getString(R.styleable.TitleView_titleTextContent);
        tv_right_content = typedArray.getString(R.styleable.TitleView_rightTextContent);
        tv_left_content = typedArray.getString(R.styleable.TitleView_leftTextContent);
        titleBackground = typedArray.getString(R.styleable.TitleView_titleBackground);
        titleTextColor = typedArray.getString(R.styleable.TitleView_titleViewTextColor);
        left_image = typedArray.getResourceId(R.styleable.TitleView_left_image, 0);
        right_image = typedArray.getResourceId(R.styleable.TitleView_right_image,0);
    }

    private void initviews(final Context context){
        View v = LayoutInflater.from(context).inflate(R.layout.titleview, null);

        iv_left = (ImageView)v.findViewById(R.id.iv_left);
        iv_left.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onIvLefteClickListener==null){
                    ((Activity) getContext()).finish();
                }else{
                    onIvLefteClickListener.onClick(v);
                }
            }
        });

        tv_left = (TextView)v.findViewById(R.id.tv_left);
        tv_left.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onTvLeftClickListener==null){
                    ((Activity) getContext()).finish();
                }else{
                    onTvLeftClickListener.onClick(v);
                }
            }
        });

        tv_title = (TextView)v.findViewById(R.id.tv_title);
        tv_title.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onTvTitleClickListener!=null){
                    onTvTitleClickListener.onClick(v);
                }
            }
        });

        tv_right = (TextView)v.findViewById(R.id.tv_right);
        tv_right.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onTvRightClickListener!=null){
                    onTvRightClickListener.onClick(v);
                }
            }
        });

        iv_right = (ImageView)v.findViewById(R.id.iv_right);
        iv_right.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onIvRightClickListener!=null){
                    onIvRightClickListener.onClick(v);
                }
            }
        });
        rl_view = (RelativeLayout) v.findViewById(R.id.ll_view);
        setShowIvLeft(show_iv_left);
        setShowTvLeft(show_tv_left);
        setShowTvTitle(show_tv_title);
        setShowTvRight(show_tv_right);
        setShowIvRight(show_iv_right);
        setTitleContent(tv_title_content);
        setRightContent(tv_right_content);
        setLeftContent(tv_left_content);
        setTitleBackground(titleBackground);
        setTitleTextColor(titleTextColor);
        setLeftImage(left_image);
        setRightImage(right_image);
        addView(v);
    }

    public ImageView getIv_right(){
        return iv_right;
    }

    public TextView getTv_right(){
        return tv_right;
    }

    public TitleView setLeftImage(int image){
        if (image!=0){
            iv_left.setImageResource(image);
        }
        return this;
    }

    public TitleView setRightImage(int image){
        if (image!=0){
            iv_right.setImageResource(image);
        }
        return this;
    }

    public TitleView setTitleTextColor(String color){
        if (!TextUtils.isEmpty(color)){
            tv_title.setTextColor(Color.parseColor(color));
        }
        return this;
    }

    public TitleView setTitleContent(String content){
    tv_title.setText(content);
        return this;
    }

    public TitleView setRightContent(String content){
        setShowIvRight(false);
        tv_right.setText(content);
        return this;
    }

    public TitleView setLeftContent(String content){
        tv_left.setText(content);
        return this;
    }

    public TitleView setShowTvLeft(boolean show){
        tv_left.setVisibility(show?VISIBLE:GONE);
        return this;
    }
    public TitleView setShowIvLeft(boolean show){
        iv_left.setVisibility(show?VISIBLE:GONE);
        return this;
    }
    public TitleView setShowTvTitle(boolean show){
        tv_title.setVisibility(show?VISIBLE:GONE);
        return this;
    }
    public TitleView setShowTvRight(boolean show){
        tv_right.setVisibility(show?VISIBLE:GONE);
        return this;
    }
    public TitleView setShowIvRight(boolean show){
        iv_right.setVisibility(show?VISIBLE:GONE);
        return this;
    }

    public TitleView setOnIvLefteClickListener(OnClickListener onClickListener){
        if (onClickListener!=null) {
            this.onIvLefteClickListener = onClickListener;
        }
        return this;
    }

    public TitleView setOnIvRightClickListener(OnClickListener onClickListener){
        if (onClickListener!=null) {
            this.onIvRightClickListener = onClickListener;
        }
        return this;
    }

    public TitleView setOnTvLeftClickListener(OnClickListener onClickListener){
        if (onClickListener!=null) {
            this.onTvLeftClickListener = onClickListener;
        }
        return this;
    }

    public TitleView setOnTvTitleClickListener(OnClickListener onClickListener) {
        if (onClickListener != null) {
            this.onTvTitleClickListener = onClickListener;
        }
        return this;
    }
    public TitleView setOnTvRightClickListener(OnClickListener onClickListener) {
        if (onClickListener != null) {
            this.onTvRightClickListener = onClickListener;
        }
        return this;
    }

    public TitleView setTitleBackground(String color){
        if (!TextUtils.isEmpty(color)){
            rl_view.setBackgroundColor(Color.parseColor(color));
        }
        return this;
    }

    public TitleView setTitleBackground(int color){
            rl_view.setBackgroundColor(color);
        return this;
    }
}
